<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑导航End -->

    <el-card>
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="9">
          <el-input
            placeholder="请输入内容"
            v-model="listParams.query"
            clearable
            @clear="searchQuery"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchQuery"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 搜索框End -->

        <!-- 添加按钮 -->
        <el-col :span="6">
          <el-button type="primary" @click="toAddGoods">添加商品</el-button>
        </el-col>
        <!-- 添加按钮End -->
      </el-row>

      <!-- 列表数据 -->
      <el-table :data="listData" border stripe class="mt-15">
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="680"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格（元）"
        ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column label="创建时间" width="200">
          <template slot-scope="scope">
            <!-- <span>{{ goodsAddTime(scope.row.add_time) }}</span> -->
            <span>{{ scope.row.add_time | timestamp2Date }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" class="el-icon-edit"></el-button>
            <el-button
              type="danger"
              class="el-icon-delete"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 列表数据End -->

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listParams.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="listParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalnum"
        class="mt-15"
        background
      >
      </el-pagination>
      <!-- 分页End -->
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      listData: [],
      // 列表请求参数
      listParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 数据总条数
      totalnum: 0,

      // 搜索关键词
      inputSearchKey: "",
    };
  },
  created() {
    // 发送请求
    console.log(this.$route, this.$router);
    this.getGoodsList();
  },
  methods: {
    // 获取列表数据
    async getGoodsList() {
      let { data: res } = await this.$http("/goods", {
        params: this.listParams,
      });
      if (res.meta.status !== 200)
        return this.$message.error("请求数据列表失败");
      this.$message.success("请求数据列表成功");
      this.totalnum = res.data.total;
      this.listData = res.data.goods;
      console.log(this.listData);
    },

    // // 商品创建时间格式化     时间戳 --> 日期时间
    // goodsAddTime(timestamp) {
    //   let time = new Date(timestamp * 1000);
    //   let year = time.getFullYear();
    //   let month = time.getMonth() + 1;
    //   month = month < 10 ? "0" + month : month;

    //   let day = time.getDate();
    //   let hours = time.getHours();
    //   let minutes = time.getMinutes();
    //   let seconds = time.getSeconds();

    //   return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    // },

    // 每页条数改变时
    handleSizeChange(pagesize) {
      this.listParams.pagesize = pagesize;
      this.getGoodsList();
    },
    // 当前页改变时
    handleCurrentChange(curPage) {
      this.listParams.pagenum = curPage;
      this.getGoodsList();
    },

    // 搜索关键字
    searchQuery() {
      // 根据关键字发送请求
      this.getGoodsList();
    },

    // 删除商品
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { data: res } = await this.$http.delete(`/goods/${id}`);
          console.log(res);
          if (res.meta.status !== 200)
            return this.$message.error("删除商品失败!");
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getGoodsList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 跳转到添加商品组件
    toAddGoods() {
      this.$router.push({
        path: "/goods/add"
      });
    },
  }, // Methods End
};
</script>

<style scoped>
</style>